//
//  LNHGoodsInfoPopView.m
//  LNH
//
//  Created by cruise on 2018/2/7.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "LNHGoodsInfoPopView.h"

#import "LNHGoodsInfoPopCell.h"

#import "LNHGoodsDetailViewModel.h"

@interface LNHGoodsInfoPopView() <UITableViewDelegate, UITableViewDataSource> {
    UIView *_rootView;
}

@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *doneBtn;

@property (nonatomic, strong) LNHGoodsDetailViewModel *viewModel;

@end

@implementation LNHGoodsInfoPopView

/**
 为视图绑定 viewModel
 
 @param viewModel 要绑定的ViewModel
 @param params 额外参数
 */
- (void)bindViewModel:(id <LMViewProtocol>)viewModel withParams:(NSDictionary *)params{
    if ([viewModel isKindOfClass:[LNHGoodsDetailViewModel class]]){
        
        self.viewModel = (LNHGoodsDetailViewModel *)viewModel;
        
        [[RACObserve(self.viewModel, skuAttrList) filter:^BOOL(NSArray *list) {
            return list.count > 0;
        }] subscribeNext:^(NSArray *list) {
            
            [self.tableView reloadData];
        }];
        
    }
    
}

/**
 初始化额外数据
 */
- (void)lm_initializeForView{
    
}

/**
 初始化视图
 */
- (void)lm_createViewForView{
    
    [self setFrame:(CGRect){ 0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT*0.7 }];
    
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.doneBtn];
    [self.contentView addSubview:self.tableView];
    
    [self setNeedsUpdateConstraints];
}

-(void) updateConstraints {
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(20);
    }];
    
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(isIPhoneX ? -34: 0);
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(40);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.bottom.equalTo(self.doneBtn.mas_top);
        make.left.right.equalTo(self.contentView);
    }];
    
    [super updateConstraints];
}

#pragma mark - getter

-(UIView *) coverView {
    if (!_coverView) {
        _coverView = [[UIView alloc] initWithFrame:[[UIApplication sharedApplication] keyWindow].bounds];
        [_coverView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.7)];
        _coverView.alpha = 0;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            [self hideView];
        }];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}

-(UIView *) contentView {
    if (!_contentView) {
        _contentView = [UIView new];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
        
    }
    return _contentView;
}

-(UILabel *) titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        [_titleLabel setTextColor:RGBCOLOR(102,102,102)];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        [_titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_titleLabel setText:@"产品参数"];
    }
    return _titleLabel;
}

-(UIButton *) doneBtn {
    if (!_doneBtn) {
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setBackgroundColor:RGBCOLOR(237, 119, 66)];
        [_doneBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_doneBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [[_doneBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self hideView];
        }];
    }
    return _doneBtn;
}

-(UITableView *) tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView setDataSource:self];
        [_tableView setDelegate:self];
        [_tableView setTableFooterView:[UIView new]];
        
        [_tableView setEstimatedRowHeight:50];
        [_tableView setRowHeight:UITableViewAutomaticDimension];
    }
    return _tableView;
}

#pragma - mark public

-(void) showView{
    
    _rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
    [_rootView addSubview:self.coverView];
    
    [[UIApplication sharedApplication].windows[0] addSubview:self];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.coverView setAlpha:0.6f];
        
        [self setFrame:(CGRect){ 0, SCREEN_HEIGHT-SCREEN_HEIGHT*0.7, SCREEN_WIDTH, SCREEN_HEIGHT*0.7 }];
        
    } completion:nil];
}

-(void) hideView {
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        [self.coverView setAlpha:0.f];
        [self setFrame:(CGRect){ 0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT*0.7 }];
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma - mark UITableViewDelegate & UITableViewDataSource

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.skuAttrList.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LNHGoodsInfoPopCell *cell = [tableView dequeueReusableCellWithIdentifier:GCN(LNHGoodsInfoPopCell)];
    
    if (cell == nil) {
        cell = [[LNHGoodsInfoPopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GCN(LNHGoodsInfoPopCell)];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *dict =  self.viewModel.skuAttrList[indexPath.row];
    
    [cell.titleLabel setText:dict[kPropertyDataKeyGoodsDetailSkuName]];
    [cell.contentLabel setText:dict[kPropertyDataKeyGoodsDetailSkuSpliceValue]];
    
   
    
    return cell;
}

@end
